package br.elsa.survey.domain.survey;

import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.PrePersist;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.NotEmpty;

import br.elsa.survey.domain.questions.Question;

/**
 * JPA Entity that abstracts a survey concept.
 * 
 * @author diego
 * @since 07/11/2010
 */
@Entity
@Table(name = "surveys", schema = "survey")
public class Survey {

	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE)
	private int id;

	@NotEmpty(message = "Survey title can not be empty")
	private String title;

	@NotEmpty(message = "Survey description can not be empty")
	private String description;

	@NotNull
	@Column(name = "creation_date")
	@Temporal(TemporalType.TIMESTAMP)
	private Date creationDate;

	@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.REFRESH)
	@JoinTable(name = "surveys_questions", schema = "survey", joinColumns = @JoinColumn(name = "survey_id"), inverseJoinColumns = @JoinColumn(name = "question_id"))
	private List<Question> questions = new ArrayList<Question>();

	@PrePersist
	public void prePersiste() {
		this.creationDate = new Date(System.currentTimeMillis());
	}

	/**
	 * Gets the questions count in the survey
	 * 
	 * @return the count of questions in the survey
	 */
	public int questionsCount() {

		return questions.size();
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public List<Question> getQuestions() {
		return questions;
	}

	public void setQuestions(List<Question> questions) {
		this.questions = questions;
	}

	public void addQuestion(Question question) {

		if (question == null) {
			throw new InvalidParameterException("The survey question can not be null.");
		}

		questions.add(question);
	}

	public void addQuestions(List<Question> questions) {

		if (questions == null) {
			throw new InvalidParameterException("The survey questions can not be null.");
		}

		this.questions.addAll(questions);
	}

	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}

	public Date getCreationDate() {
		return creationDate;
	}

	public void removeQuestion(Question question) {
		this.questions.remove(question);
	}
}
